package com.amkai.aes.webservice.binary.objecthandler;

import java.util.List;

import com.amkai.aes.aescommon.service.binary.job.BatchValidatorJob;
import com.amkai.aes.aescommon.service.result.ResultObject;
import com.amkai.aes.generatedbeans.billinginterval.Batch;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class BatchValidatorHandler implements JobObjectHandler<BatchValidatorJob,Batch,ResultObject>{

	@Override
	public ResultObject process(BatchValidatorJob job, Batch batch) throws Exception {
		SessionManager sm = SiteManager.Start();
		ResultObject result = new ResultObject();
		long id = 0;
		if (batch.getId() != null)
			id = batch.getId().longValue();
		
		List<Batch> batches = sm.execute("FROM Batch b WHERE b.description=? AND b.period.baseOrganization.id=? AND b.id<>?").setString(0,batch.getDescription()).setLong(1,batch.getPeriod().getBaseOrganization().getId().longValue()).setLong(2,id).list();
		if (batches.size()>0) {
			result.setStatusCode(1);
			result.setMessage("\" "+batch.getDescription()+" \" batch name is used by another batch in this department!");
		}
		return result;
	}

}
